﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using devtm.Configuration;
using System.Collections;
using devtm.Editor.Configuration.Command;
using System.Collections.Specialized;
using System.Reflection;

namespace devtm.Editor.Configuration.Data
{
    
    public enum typeContainer
    {
        Unknow,
        NameValueCollection,
        StringCollection
    }

    public class SimpleProperty : INotifyPropertyChanged
    {


        public SimpleProperty()
        {
            this.PropertyChanged += SimpleProperty_PropertyChanged;
        }


        public string _value;
        public string Value
        {
            get
            {
                return _value;
            }
            set
            {

                if (_value == value)
                    return;

                if (Parent != null)
                    ChangeValue(_value , value);
                

                _value = value;

                ChangeProperty("Value");

            }
        }

        private void ChangeValue(string oldValue, string newValue)
        {

            switch (TypeContainer)
            {

                case typeContainer.NameValueCollection:
                    ChangeValue(Container as NameValueCollection, oldValue, newValue);
                    break;

                case typeContainer.StringCollection:
                    ChangeValue(Container as StringCollection, oldValue, newValue);
                    break;

                case typeContainer.Unknow:
                default:
                    break;
                    
            }

        }

        private void ChangeValue(StringCollection list, string oldValue, string newValue)
        {

            string key = Parent.Key;
            
            foreach (var item in list)
            {
                
            }

        }

        private void ChangeValue(NameValueCollection list, string oldValue, string newValue)
        {

            string key = Parent.Key;

            list[key] = newValue;

            CommandHelpers.HasCollectionUpdated(ElementSection,list, key, oldValue, newValue, TypeChange.Change);

        }


        public ItemPair Parent { get; set; }
        public event PropertyChangedEventHandler PropertyChanged;
        public IElementSection ElementSection { get; set; }
        public ICollection Container { get; set; }
        public typeContainer TypeContainer { get; set; }

        public void ChangeProperty(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        void SimpleProperty_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {

            if (e.PropertyName == "Value" && Template != null)
            {
                MethodInfo m = Template.MethodIsValid;
                if (m != null)
                    m.Invoke(null, new object[] { this });
            }

        }

        public devtm.Configuration.Models.PropertyItemAttribute Template { get; set; }
    }
}
